//
//  CommentCell.m
//  ZhiHuDaily
//
//  Created by albert on 16/8/18.
//  Copyright © 2016年 albert. All rights reserved.
//

#import "CommentCell.h"
#import "CommentModel.h"
#import "CommentRefModel.h"
#import "UIImageView+JKExtension.h"
#import "JKTimeStampTool.h"

@interface CommentCell ()
/** 头像 */
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;

/** 夜间模式遮盖头像的imagView */
@property (weak, nonatomic) IBOutlet UIImageView *iconMaskImgView;

/** 用户名 */
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

/** 评论内容 */
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;

/** 引用的评论 */
@property (weak, nonatomic) IBOutlet UILabel *refContentLabel;

/** 时间 */
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

/** 点赞数量label */
@property (weak, nonatomic) IBOutlet UILabel *zanLabel;

/** 点赞后的动画label */
@property (weak, nonatomic) IBOutlet UILabel *zanAnimationLabel;

/** 点赞图标btn */
@property (weak, nonatomic) IBOutlet UIButton *zanIconBtn;

/** unfoldButton */
@property (weak, nonatomic) IBOutlet UIButton *unfoldButton;

/** 当前皮肤模式 */
@property (nonatomic, copy) NSString *currentSkinModel;

/** 夜间模式选中背景view */
@property (nonatomic, weak) UIView *selectedBgViewNight;

/** 日间模式选中背景view */
@property (nonatomic, weak) UIView *selectedBgViewDay;
@end

@implementation CommentCell

- (UIView *)selectedBgViewDay{
    if (!_selectedBgViewDay) {
        UIView *vvDay = [[UIView alloc] init];
        vvDay.backgroundColor = JKColor(217, 217, 217);
        [self.contentView addSubview:vvDay];
        _selectedBgViewDay = vvDay;
    }
    return _selectedBgViewDay;
}

- (UIView *)selectedBgViewNight{
    if (!_selectedBgViewNight) {
        UIView *vvNight = [[UIView alloc] init];
        vvNight.backgroundColor = JKColor(40, 40, 40);
        [self.contentView addSubview:vvNight];
        _selectedBgViewNight = vvNight;
    }
    return _selectedBgViewNight;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.iconMaskImgView.image = [[UIImage imageNamed:@"icon_mask"] circleImage];
}

- (void)setCmtModel:(CommentModel *)cmtModel{
    _cmtModel = cmtModel;
    
    [self.iconImageView setHeaderIcon:cmtModel.avatar];
    self.nameLabel.text = cmtModel.author;
    self.contentLabel.attributedText = cmtModel.contentAttr;
    self.timeLabel.text = [JKTimeStampTool timeStrWithTimeStamp:cmtModel.time];
    self.zanLabel.text = @(cmtModel.likes).stringValue;
    self.zanLabel.textColor = cmtModel.isLiked ? JKMainColor : JKColor(128, 128, 128);
    self.zanIconBtn.selected = cmtModel.isLiked ? YES : NO;
    self.unfoldButton.hidden = YES;
    self.refContentLabel.hidden = (cmtModel.reply_to == nil);
    
    // 引用评论为空 直接返回
    if (cmtModel.reply_to == nil) return;
    
    // 引用评论不为空
    self.currentSkinModel = [[NSUserDefaults standardUserDefaults] objectForKey:JKCurrentSkinModelKey];
    
    if (cmtModel.reply_to.status != 0) { // 引用评论已被删除
        
        self.refContentLabel.attributedText = [[NSAttributedString alloc] initWithString:@"  抱歉，原点评已经被删除"];
        self.refContentLabel.backgroundColor = [self.currentSkinModel isEqualToString:JKDaySkinModelValue] ? JKColor(233, 233, 233) : JKColor(38, 38, 38);
        self.refContentLabel.textColor = [self.currentSkinModel isEqualToString:JKDaySkinModelValue] ? [UIColor blackColor] : JKColor(118, 118, 118);
        self.refContentLabel.font = [UIFont systemFontOfSize:12];
        
    }else { // 引用评论未删除
        
        // 展开收起按钮的显示和隐藏
        self.unfoldButton.hidden = (_cmtModel.reply_to.cmtContentRefH > 55) ? NO : YES;
        
        // 用模型控制一切！！
        NSInteger lines = (_cmtModel.reply_to.cmtContentRefH > 55) ? 2 : 3;
        self.refContentLabel.numberOfLines = cmtModel.cmtRefUnfolded ? 0 : lines;
        self.unfoldButton.selected = cmtModel.cmtRefUnfolded;
        
        self.refContentLabel.backgroundColor = [UIColor clearColor];
        self.refContentLabel.font = [UIFont systemFontOfSize:14];
        
        // 4.显示字符串
        self.refContentLabel.attributedText = [self.currentSkinModel isEqualToString:JKDaySkinModelValue] ? _cmtModel.reply_to.contentRefAttr : _cmtModel.reply_to.contentRefNightAttr;
    }
}

- (IBAction)unfoldButtonClick:(UIButton *)button {
    
    button.selected = !button.selected;
    
    if ([self.delegate respondsToSelector:@selector(commentCell:didClickUnfoldButton:)]) {
        [self.delegate commentCell:self didClickUnfoldButton:button];
    }
}

/** 点赞 */
- (void)zanClick{
    self.zanLabel.text = @(self.cmtModel.likes).stringValue;
    self.zanLabel.textColor = self.cmtModel.isLiked ? JKMainColor : JKColor(128, 128, 128);
    self.zanIconBtn.selected = self.cmtModel.isLiked ? YES : NO;
    
    if (!self.cmtModel.isLiked) return;
    self.zanAnimationLabel.hidden = NO;
    self.zanAnimationLabel.transform = CGAffineTransformMakeScale(1.5, 1.5);
    [UIView animateWithDuration:1 animations:^{
        self.zanAnimationLabel.transform = CGAffineTransformScale(self.zanAnimationLabel.transform, 0.1, 0.1);
        self.zanAnimationLabel.transform = CGAffineTransformTranslate(self.zanAnimationLabel.transform, 0, 110);
    } completion:^(BOOL finished) {
        self.zanAnimationLabel.hidden = YES;
    }];
}

/** 计算引用评论label高度 */
- (CGFloat)calculateRefContentLabelHeightModel:(CommentModel *)cmtModel{
    // 1.设置数据
    self.cmtModel = cmtModel;
    
    // 2.更新UI
    [self layoutIfNeeded];
    
    // 3.返回底部视图的最大值
    return self.refContentLabel.height;
}

#pragma mark - UIMenuController
/** 能成为第一响应者 */
- (BOOL)canBecomeFirstResponder{
    return YES;
}

/** 能执行哪些操作（copy、paste...） */
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{
    //让控制器称为第一响应者必须这样？？！！
    //if (action == @selector(ding:) || action == @selector(respond:) || action == @selector(report:)) return YES;
    return NO;
}

#pragma mark - 更新皮肤
- (void)changeToNightSkin{ // 夜间
    self.backgroundColor = JKNightSkinColor;
    self.iconMaskImgView.hidden = NO;
    self.nameLabel.textColor = JKColor(186, 186, 186);
    self.contentLabel.textColor = JKColor(153, 153, 153);
    self.timeLabel.textColor = JKColor(108, 108, 108);
    [self.unfoldButton setBackgroundColor:JKColor(38, 38, 38)];
    self.separatorLineView.backgroundColor = JKColor(103, 103, 103);
    
    self.selectedBackgroundView = self.selectedBgViewNight;
}

- (void)changeToDaySkin{ // 白天
    self.backgroundColor = JKGlobalBgColor;
    self.iconMaskImgView.hidden = YES;
    self.nameLabel.textColor = JKColor(53, 53, 53);
    self.contentLabel.textColor = JKColor(53, 53, 53);
    self.timeLabel.textColor = JKColor(118, 118, 118);
    [self.unfoldButton setBackgroundColor:JKColor(203, 217, 233)];
    self.separatorLineView.backgroundColor = JKColor(217, 217, 217);
    
    self.selectedBackgroundView = self.selectedBgViewDay;
}
@end
